//
//  ListDetailViewController.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "ListDetailViewController.h"

@interface ListDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ListDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    [self creatTableView];

}

- (void)getData {
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    
    [SANetWorkingTask request:self.url parmater:nil block:^(id result) {
        
        NSArray *array = result[@"list"];
        
        if ([self.contentType isEqualToString:@"track"]) {
            
            for (NSDictionary *dic in array) {
                listDetailModel1 *model = [[listDetailModel1 alloc] initWithDictionary:dic];
               
                model.coverMiddle = model.coverSmall;
                [self.dataSource addObject:model];
                
            }
            
        } else if ([self.contentType isEqualToString:@"album"]) {
            
            
            for (NSDictionary *dic in array) {
                listModel2 *model = [[listModel2 alloc] initWithDictionary:dic];
                [self.dataSource addObject:model];
            }

        } else {
            
            for (NSDictionary *dic in array) {
                listModel3 *model = [[listModel3 alloc] initWithDictionary:dic];
                [self.dataSource addObject:model];
            }
        }
        [self.tableView reloadData];
    }];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    [self.tableView registerClass:[ListTableViewCell1 class] forCellReuseIdentifier:@"ListTableViewCell1"];
    [self.tableView registerClass:[ListTableViewCell2 class] forCellReuseIdentifier:@"ListTableViewCell2"];
    [self.tableView registerClass:[ListTableViewCell3 class] forCellReuseIdentifier:@"ListTableViewCell3"];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.contentType isEqualToString:@"track"]) {
        ListTableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"ListTableViewCell1"];
        cell.model = self.dataSource[indexPath.row];
        return cell;
    } else if ([self.contentType isEqualToString:@"album"]) {
        ListTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"ListTableViewCell2"];
        cell.model = self.dataSource[indexPath.row];
        return cell;
    } else {
        ListTableViewCell3 *cell = [tableView dequeueReusableCellWithIdentifier:@"ListTableViewCell3"];
        cell.model = self.dataSource[indexPath.row];
        return cell;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataSource.count) {
        return self.dataSource.count;
    }
    return 0;
}

#pragma mark -- tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 100;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

    if ([self.contentType isEqualToString:@"track"]) {
        PlayViewController *playVC = [PlayViewController sharePlayViewController];
        playVC.dataSource = [NSMutableArray arrayWithArray:self.dataSource];
        
        playVC.sequence = indexPath.row;
        
        [self presentViewController:playVC animated:YES completion:^{
            
            
        }];
        
    } else if ([self.contentType isEqualToString:@"album"]) {
        AlbumViewController *albumVC = [[AlbumViewController alloc] init];
        albumVC.hotRecommendListModel = [[HotRecommendListModel alloc] init];
        albumVC.hotRecommendListModel.albumId = [self.dataSource[indexPath.row] albumId];
        [self.navigationController pushViewController:albumVC animated:YES];
        
        
    } else {
        
        
        
    }
}


#pragma mark -- 刷新

//- (void)addheader {
//    
//    [self.tableView addHeaderWithCallback:^{
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self getData];
//            [self.tableView headerEndRefreshing];
//        });
//            
//    }];
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
